package org.king.service.impl;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.king.entity.Activity;
import org.king.entity.vo.ActivityEntity;
import org.king.entity.vo.Comment;
import org.king.global.SessionContext;
import org.king.mapper.ActivityMapper;
import org.king.service.ActivityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Resource
    private ActivityMapper activityMapper;

    /**
     * 通过活动id获得活动
     * @param activityId 活动id
     * @return 活动
     */
    @Override
    public ActivityEntity getActivity(int activityId) {
        return new ActivityEntity(activityMapper.getActivity(activityId));
    }

    /**
     * 获得所有的活动
     * @return 所有活动列表
     */
    @Override
    public ObservableList<ActivityEntity> getAllActivities() {
        List<Activity> dbActivities = activityMapper.getAllActivities();
        return dbActivities.stream()
                .map(ActivityEntity::new) // 使用构造函数转换
                .collect(Collectors.toCollection(FXCollections::observableArrayList));
    }

    /**
     * 用户自己发布的活动
     * @param userId 用户id
     * @return 活动列表
     */
    @Override
    public ObservableList<ActivityEntity> getActivitiesByUserId(int userId) {
        List<Activity> dbActivities = activityMapper.getUserActivities(userId);
        return dbActivities.stream()
                .map(ActivityEntity::new) // 使用构造函数转换
                .collect(Collectors.toCollection(FXCollections::observableArrayList));
    }

    /**
     * 添加活动
     * @param activity 活动实体
     * @return 是否创建成功
     */
    @Override
    public int addActivity(Activity activity) {
        activity.setCreate_id(SessionContext.getInstance().getCurrentUser().getId());
        return activityMapper.addActivity(activity);
    }

    /**
     * 删除活动
     * @param activityId 活动id
     * @return 是否成功删除
     */
    @Override
    public int deleteActivity(int activityId) {
        return activityMapper.deleteActivity(activityId);
    }

    /**
     * 更新活动
     * @param activity 活动实体
     * @return 影响的行数，是否更新成功
     */
    @Override
    public int updateActivity(Activity activity) {
        return activityMapper.updateActivity(activity.getName(),
                activity.getActivity_describe(),
                activity.getBegin_time(),
                activity.getEnd_time(),
                activity.getAddress(),
                activity.getActivity_id());
    }

    /**
     * 添加用户参加活动表
     * @param userId 用户id
     * @param activityId 活动id
     * @return 是否插入成功
     */
    @Override
    public int addUserJoinActivity(int userId, int activityId) {
        return activityMapper.addUserJoinActivity(userId, activityId);
    }

    /**
     * 更新用户参加活动
     * @param activityId 活动id
     * @param userId 用户id
     * @param score 分数
     * @return 是否更新成功
     */
    @Override
    public int updateUserJoinActivityScore(int activityId, int userId, int score) {
        return activityMapper.updateUserJoinActivityScore(activityId, userId, score);
    }

    /**
     * 删除用户参加活动
     * @param activityId 活动id
     * @param userId 用户id
     * @return 是否删除成功
     */
    @Override
    public int deleteUserJoinActivity(int activityId, int userId) {
        return activityMapper.deleteUserJoinActivity(activityId, userId);
    }

    /**
     * 查询用户是否参加了活动
     * @param activityId 活动id
     * @param userId 用户id
     * @return 是否成功参与
     */
    @Override
    public int getUserJoinActivity(int activityId, int userId) {
        return activityMapper.getUserJoinActivity(activityId, userId);
    }

    /**
     * 根据日期获得活动
     * @param searchDate 日期
     * @return 活动列表
     */
    @Override
    public ObservableList<ActivityEntity> getActivitiesByDate(LocalDate searchDate) {
        LocalDateTime startOfDay = searchDate.atStartOfDay();
        LocalDateTime endOfDay = searchDate.atTime(LocalTime.MAX);

        List<ActivityEntity> activities = activityMapper.findByDateRange(startOfDay, endOfDay);
        return FXCollections.observableArrayList(activities);
    }

    /**
     * 活动参与人数
     * @param activityId 活动id
     * @return 参与人数
     */
    @Override
    public int getParticipantCount(int activityId) {
        return activityMapper.getParticipantCount(activityId);
    }

    /**
     * 活动的平均评分
     * @param activityId 活动id
     * @return 活动的平均分
     */
    @Override
    public double getAverageRating(int activityId) {
        Double avg = activityMapper.getAverageRating(activityId);
        return avg != null ? avg : 0.0; // 如果无评分，返回0.0
    }

    /**
     * 新增评论
     * @param comment 评论
     * @return 是否插入成功
     */
    @Override
    public int insertComment(Comment comment) {
        return activityMapper.addComment(comment);
    }

    /**
     * 更新评论
     * @param comment 评论内容0
     * @param CommentId 评论的id
     * @return 是否成功更新评论
     */
    @Override
    public int updateComment(String comment, int CommentId) {
        return activityMapper.updateComment(comment,CommentId);
    }

    /**
     * 查询用户的所有评论
     *
     * @param ActivityId 活动id
     * @return 返回用户的评论列表
     */
    @Override
    public List<Comment> getCommentsByActivity(int ActivityId) {
        return activityMapper.getCommentByActivity(ActivityId);
    }

}
